package com.example.mongodb.controller;


import com.example.mongodb.dao.ExchangeOrderDeatilRepository;
import com.example.mongodb.entity.ExchangeOrderDetail;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.Decimal128;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
public class ExchangeOrderDeatilController {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private ExchangeOrderDeatilRepository exchangeOrderDeatilRepository;

    @GetMapping("test")
    public String test(){
        log.info("保存两条数据");
        BigDecimal bigDecimal = new BigDecimal("11111111111111113.00000000000000009",MathContext.DECIMAL128);
//        exchangeOrderDeatilRepository.save(ExchangeOrderDetail.builder()
//                .orderId("123")
//                .amount(bigDecimal)
//                .fee(BigDecimal.valueOf(0.00000000000000008))
//                .money("111111111111111113.000000000000000009")
//                .date(new Date())
//                .build());
//        exchangeOrderDeatilRepository.save(ExchangeOrderDetail.builder()
//                .orderId("123")
//                .amount(BigDecimal.valueOf(0.00000000000000008))
//                .fee(BigDecimal.valueOf(0.00000000000000008))
//                .date(new Date())
//                .build());
        log.info("通过接口查询数据");
        List<ExchangeOrderDetail> allByOrderId = exchangeOrderDeatilRepository.findAllByOrderId("123");
        for (ExchangeOrderDetail exchangeOrderDetail : allByOrderId) {
            System.out.println(exchangeOrderDetail.toString());
        }

        log.info("通过mongoTemplate查询数据");
        Query query = new Query(Criteria.where("orderId").is("123"));
        List<ExchangeOrderDetail> exchangeOrderDetails = mongoTemplate.find(query, ExchangeOrderDetail.class);
        for (ExchangeOrderDetail exchangeOrderDetail : exchangeOrderDetails) {
            System.out.println(exchangeOrderDetail.toString());
        }
        return "succ";
    }

    public static void main(String[] args) {
//        BigDecimal bigDecimal = new BigDecimal("1111111111111111111113.0000000000000000000008");
//        System.out.println(bigDecimal);
        Decimal128 decimal128 = new Decimal128(new BigDecimal("1.0000000000000000000000000000000007"));
        System.out.println(decimal128);
    }
}
